package dao.impl;

import dao.UserDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.User;

import java.sql.SQLException;
import java.util.List;
/**
 * dao层类，持久层类
 * 操作数据库，依赖QueryRunner类
 */
public class UserDaoImpl implements UserDao {
    //不要new对象，通过配置文件中的对象，注入进来
    private QueryRunner qr;

    public UserDaoImpl(QueryRunner qr) {
        this.qr = qr;
    }

    @Override
    public int insertUser(User user) throws SQLException {
        String sql = "insert into user values (?,?,?,?,?)";
        //取出User对象中的参数，建议将User对象中的参数，存在数组里面
        Object[] params = {null,user.getUsername(),user.getSex(),user.getBirthday(),user.getAddress()};
        return qr.update(sql,params);
    }

    @Override
    public List<User> queryUser() throws SQLException {
        String sql = "select * from user";
        return qr.query(sql,new BeanListHandler<User>(User.class));
    }
}
